//
//  AdvertisingManager.m
//  iModern
//
//  Created by Ralf on 5/13/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

@import UIKit;

#import "AdvertisingManager.h"
#import "AttachmentManager.h"
#import "AppDelegate.h"

@implementation AdvertisingManager

- (ResultSet *)createAdvertising:(NSDictionary *)advertising
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    params[@"type"]      = @"Article";
    params[@"subltype"]  = @"Advertising";
    params[@"sessionID"] = [self getSessionID];

    params[@"indexOfSaleTags"] = advertising[@"indexOfSaleTags"];
    params[@"summary"]         = advertising[@"summary"];
    params[@"message"]         = advertising[@"message"];
    params[@"price"]           = advertising[@"price"];
    params[@"amountOfStocks"]  = advertising[@"amountOfStocks"];
    params[@"preset"]          = advertising[@"preset"];
    params[@"pmi"]             = advertising[@"pmi"];
    params[@"pom"]             = advertising[@"pom"];
    params[@"story"]           = advertising[@"story"];
    //    params[@"pickup"]          = advertising[@"pickup"];
    //    params[@"cityExpress"]     = advertising[@"cityExpress"];
    //    params[@"nationalExpress"] = advertising[@"nationalExpress"];

    NSData   *dImage = UIImageJPEGRepresentation(advertising[@"image"], 0.6);
    NSData   *data   = [dImage base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    NSString *sImage = [NSString stringWithUTF8String:[data bytes]];
    params[@"image"] = sImage;

    NSString *mediaType = advertising[@"mediaType"];

    if (mediaType == nil)
    {
        mediaType = @"Image";
    }

    [params setValue:mediaType forKey:@"mediaType"];

    if ([mediaType isEqualToString:@"Video"])
    {
        NSData *video = advertising[@"video"];
        data = [video base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
        params[@"video"] = string;
    }

    ResultSet *rs = [self requestJSON:@"article/createAdvertising" params:params];

    if (rs != nil && ![rs hasErrors])
    {
        NSMutableArray *images = advertising[@"images"];
        if (images.count > 0)
        {
            NSDictionary      *advertising       = [rs toDictionary];
            NSString          *articleId         = advertising[@"id"];
            AttachmentManager *attachmentManager = [AttachmentManager new];
            NSMutableDictionary *image;
            for (int i=0;i<images.count;i++)
            {
                image = [images objectAtIndex:i];
                [attachmentManager uploadImage:articleId image:image[@"image"] desc:image[@"description"]];
            }
        }
    }

    return rs;
}

- (ResultSet *)queryAdvertisingByUserName:(NSString *)userName max:(NSInteger)max offset:(NSInteger)offset;
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    params[@"userName"] = userName;
    params[@"max"]      = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]   = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    return [self request:@"article/queryAdvertisingByUserName" params:params];
}

- (ResultSet *)queryAdvertising:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    AppDelegate *appDelegate = [self getAppDelegate];
    if ([appDelegate isLogged])
    {
        [params setValue:[self getSessionID] forKey:@"sessionID"];
    }

    params[@"max"]    = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"] = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    return [self request:@"article/queryAdvertising" params:params];
}

- (ResultSet *)disableAdvertising:(NSString *)advertisingID
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    params[@"advertisingID"] = advertisingID;

    return [self request:@"article/disableAdvertising" params:params];
}

- (ResultSet *)enableAdvertising:(NSString *)advertisingID
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    
    [params setValue:[self getSessionID] forKey:@"sessionID"];
    params[@"advertisingID"] = advertisingID;
    
    return [self request:@"article/enableAdvertising" params:params];
}

- (ResultSet *)updateAdvertising:(NSString *)advertisingID amountOfStocks:(NSInteger)amountOfStocks
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    params[@"advertisingID"]  = advertisingID;
    params[@"amountOfStocks"] = [[NSString alloc] initWithFormat:@"%ld", (long) amountOfStocks];

    return [self request:@"article/updateAdvertising" params:params];
}

@end
